package com.pd.backend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pd.backend.dtos.PdTruckTypeDto;
import com.pd.backend.dtos.ResponseResult;
import com.pd.backend.dtos.TruckTypeDto;
import com.pd.backend.enums.AppHttpCodeEnum;
import com.pd.backend.mapper.TruckMapper;
import com.pd.backend.pojo.PdGoodsType;
import com.pd.backend.service.TruckService;
import com.pd.backend.utils.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 车辆类型业务层
 *
 * @author wjj
 * @date 2023/1/11 11:16
 **/
@Service
@Transactional
@Slf4j
public class TruckServiceImpl implements TruckService {

    @Autowired
    private TruckMapper truckMapper;

    /**
     * 新增车辆类型
     *
     * @param pdTruckTypeDto
     * @return
     */
    @Override
    public ResponseResult insertTruckType(PdTruckTypeDto pdTruckTypeDto) {
        //根据雪花算法生成id
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        String id = String.valueOf(idWorker.nextId());
        pdTruckTypeDto.setId(id);
        //获取联表pd_goods_type信息
        List<PdGoodsType> pdGoodsTypes = pdTruckTypeDto.getPdGoodsTypes();
        //插入pd_truck_type信息
        boolean result = truckMapper.insertTruckType(pdTruckTypeDto);
        //插入pd_goods_type信息
        for (PdGoodsType pdGoodsType : pdGoodsTypes) {
            String name = pdGoodsType.getName();
            truckMapper.insertGoodsType(id,name);
        }
        //判断是否有返回值
        if (result == false) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询车辆管理
     *
     * @param truckTypeDto
     * @return
     */
    @Override
    public ResponseResult selectAllTruckType(TruckTypeDto truckTypeDto) {
        //获取请求参数
        Integer page = truckTypeDto.getPage();
        Integer size = truckTypeDto.getSize();
        //设置分页
        PageHelper.startPage(page, size);

        String name = truckTypeDto.getName();
        //查询数据
        List<PdTruckTypeDto> pdTruckTypeDtos = truckMapper.selectAllTruckType(name);
        PageInfo pageInfo = new PageInfo(pdTruckTypeDtos);
        //获取分页数据
        List list = pageInfo.getList();

        //返回数据
        return ResponseResult.okResult(list);
    }

    /**
     * 修改车队管理
     *
     * @param pdTruckTypeDto
     * @return
     */
    @Override
    public ResponseResult updateTruckType(PdTruckTypeDto pdTruckTypeDto) {
        //校验参数
        if (pdTruckTypeDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //处理业务
        String id = pdTruckTypeDto.getId();
        //根据id回显数据
        PdTruckTypeDto pdTruckTypeDtoDb = truckMapper.selectTruckTypeById(id);
        //判断数据库是否为空
        if (Objects.isNull(pdTruckTypeDtoDb)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //修改数据
        truckMapper.updateTruckType(pdTruckTypeDto);
        //返回响应
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除车型
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteTruckType(String id) {
        boolean result = truckMapper.deleteTruckTypeById(id);
        if (result==false) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
